/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.ClientPhone;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.Modem;
import br.com.jr2tecnologia.model.entity.SmsSend;
import br.com.jr2tecnologia.model.repository.SmsSendRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author ronildo
 */
public class SmsSendDAO extends GenericDAO<SmsSend, SmsSendRepository> implements SmsSendRepository {

    private static final Logger logger = LoggerFactory.getLogger();

    public SmsSend findWhoSentLastSms(String phone){
        try{
            String hql = "select smsSend from SmsSend as smsSend ";
            hql+=" left outer join smsSend.login as login";
            hql+=" left outer join smsSend.clientPhone as clientPhone";
            hql+=" where clientPhone.phone = :phone ";
            hql+=" order by smsSend.datetimeRegistered desc";

            Query query = em.createQuery(hql);
            query.setParameter("phone", phone);
            query.setMaxResults(1);

            return (SmsSend)query.getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find data");
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public SmsSend findByNowSmsId(String nowSmsId) {
        try{
            return (SmsSend)em.createNamedQuery("SmsSend.findByNowSmsId").
                setParameter("nowSmsId", nowSmsId).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find data");
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public List<SmsSend> list(SmsSend smsSend){
        try{
            Login login = smsSend.getLogin();
            ClientPhone clientPhone = smsSend.getClientPhone();
            Modem modem = smsSend.getModem();

            String hql = "select smsSend from SmsSend as smsSend ";
            hql+=" left outer join smsSend.modem as modem";
            hql+=" left outer join smsSend.login as login";
            hql+=" left outer join smsSend.clientPhone as clientPhone";
            hql+=" where smsSend.pkSmsSend = smsSend.pkSmsSend";

            if(smsSend.getMessage() != null && !smsSend.getMessage().equals(""))
                hql+=" and smsSend.message like :smsSend_message";
            if(modem != null && modem.getPkModem() != null)
                hql+=" and modem.pkModem = :pkModem";
            if(login != null && login.getPkLogin() != null)
                hql+=" and login.pkLogin = :pkLogin";
            if(clientPhone != null && clientPhone.getPkClientPhone() != null)
                hql+=" and clientPhone.pkClientPhone = :pkClientPhone";
            if(clientPhone != null && clientPhone.getName() != null && !clientPhone.getName().equals(""))
                hql+=" and clientPhone.name like :clientPhone_name";
            if(clientPhone != null && clientPhone.getPhone() != null && !clientPhone.getPhone().equals(""))
                hql+=" and clientPhone.phone like :clientPhone_phone";
            hql+=" order by smsSend.datetimeRegistered";

            Query query = em.createQuery(hql);
            if(smsSend.getMessage() != null && !smsSend.getMessage().equals(""))
                query.setParameter("smsSend_message", smsSend.getMessage()+"%");
            if(modem != null && modem.getPkModem() != null)
                query.setParameter("pkModem", modem.getPkModem());
            if(login != null && login.getPkLogin() != null)
                query.setParameter("pkLogin", login.getPkLogin());
            if(clientPhone != null && clientPhone.getPkClientPhone() != null)
                query.setParameter("pkClientPhone", clientPhone.getPkClientPhone());
            if(clientPhone != null && clientPhone.getName() != null && !clientPhone.getName().equals(""))
                query.setParameter("clientPhone_name", clientPhone.getName()+"%");
            if(clientPhone != null && clientPhone.getPhone() != null && !clientPhone.getPhone().equals(""))
                query.setParameter("clientPhone_phone", clientPhone.getPhone()+"%");

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list sms send",e);
            throw new DAOException(e);
        }
    }

    public List<SmsSend> listGrouped(SmsSend smsSend, String group[]){
        try{
            Login login = smsSend.getLogin();
            ClientPhone clientPhone = smsSend.getClientPhone();
            Modem modem = smsSend.getModem();

            String hql = "select smsSend from SmsSend as smsSend ";
            hql+=" left outer join smsSend.modem as modem";
            hql+=" left outer join smsSend.login as login";
            hql+=" left outer join smsSend.clientPhone as clientPhone";
            hql+=" where smsSend.pkSmsSend = smsSend.pkSmsSend";

            if(smsSend.getMessage() != null && !smsSend.getMessage().equals(""))
                hql+=" and smsSend.message like :smsSend_message";
            if(modem != null && modem.getPkModem() != null)
                hql+=" and modem.pkModem = :pkModem";
            if(login != null && login.getPkLogin() != null)
                hql+=" and login.pkLogin = :pkLogin";
            if(clientPhone != null && clientPhone.getPkClientPhone() != null)
                hql+=" and clientPhone.pkClientPhone = :pkClientPhone";
            if(clientPhone != null && clientPhone.getName() != null && !clientPhone.getName().equals(""))
                hql+=" and clientPhone.name like :clientPhone_name";
            if(clientPhone != null && clientPhone.getPhone() != null && !clientPhone.getPhone().equals(""))
                hql+=" and clientPhone.phone like :clientPhone_phone";

            hql+=" GROUP BY clientPhone.phone";
            for(int i = 0; i < group.length; i++){
                hql += (i == 0 ? group[i] : (","+group[i]));
            }
            hql+=" order by smsSend.datetimeRegistered";

            Query query = em.createQuery(hql);
            if(smsSend.getMessage() != null && !smsSend.getMessage().equals(""))
                query.setParameter("smsSend_message", smsSend.getMessage()+"%");
            if(modem != null && modem.getPkModem() != null)
                query.setParameter("pkModem", modem.getPkModem());
            if(login != null && login.getPkLogin() != null)
                query.setParameter("pkLogin", login.getPkLogin());
            if(clientPhone != null && clientPhone.getPkClientPhone() != null)
                query.setParameter("pkClientPhone", clientPhone.getPkClientPhone());
            if(clientPhone != null && clientPhone.getName() != null && !clientPhone.getName().equals(""))
                query.setParameter("clientPhone_name", clientPhone.getName()+"%");
            if(clientPhone != null && clientPhone.getPhone() != null && !clientPhone.getPhone().equals(""))
                query.setParameter("clientPhone_phone", clientPhone.getPhone()+"%");

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list sms send",e);
            throw new DAOException(e);
        }
    }

    public List<SmsSend> listAll(){
        try{
            return em.createNamedQuery("SmsSend.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }
}
